Inviter.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 数据层模型
  13. */
  14. class Inviter extends BaseModel
  15. {
  16. public $page_info;
  17. /**
  18. * 增加帮助
  19. * @access public
  20. * @author csdeshang
  21. * @param type $inviter_array 帮助内容
  22. * @param type $upload_ids 更新ID
  23. * @return type
  24. */
  25. public function addInviter($inviter_array)
  26. {
  27. $inviter_id = Db::name('inviter')->insertGetId($inviter_array);
  28. return $inviter_id;
  29. }
  30. /**
  31. * 修改帮助记录
  32. * @access public
  33. * @author csdeshang
  34. * @param type $condition 条件
  35. * @param type $data 数据
  36. * @return boolean
  37. */
  38. public function editInviter($condition, $data)
  39. {
  40. if (empty($condition)) {
  41. return false;
  42. }
  43. if (is_array($data)) {
  44. $result = Db::name('inviter')->where($condition)->update($data);
  45. return $result;
  46. } else {
  47. return false;
  48. }
  49. }
  50. public function getInviterInfo($condition, $fields = 'm.member_name,i.*')
  51. {
  52. if (empty($condition)) {
  53. return false;
  54. }
  55. $result = Db::name('inviter')->alias('i')->join('member m', 'i.inviter_id=m.member_id')->field($fields)->where($condition)->find();
  56. return $result;
  57. }
  58. /**
  59. * 帮助记录
  60. * @access public
  61. * @author csdeshang
  62. * @param type $condition 条件
  63. * @param type $pagesize 分页
  64. * @param type $limit 限制
  65. * @param type $fields 字段
  66. * @return array
  67. */
  68. public function getInviterList($condition = array(), $pagesize = '', $limit = 0, $fields = '*')
  69. {
  70. if ($pagesize) {
  71. $res = Db::name('inviter')->alias('i')->join('member m', 'i.inviter_id=m.member_id')->field($fields)->where($condition)->order('inviter_applytime desc')->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  72. $this->page_info = $res;
  73. $result = $res->items();
  74. } else {
  75. $result = Db::name('inviter')->alias('i')->join('member m', 'i.inviter_id=m.member_id')->field($fields)->where($condition)->limit($limit)->order('inviter_applytime desc')->select()->toArray();
  76. }
  77. return $result;
  78. }
  79. /**
  80. * 生成微信推荐二维码
  81. * @param type $member_info
  82. * @return type
  83. */
  84. public function qrcode_weixin($member_info)
  85. {
  86. $wx_error_msg = '';
  87. if (!file_exists(BASE_UPLOAD_PATH . '/' . ATTACH_INVITER . '/' . $member_info['member_id'] . '_weixin.png')) {
  88. $config = model('wechat')->getOneWxconfig();
  89. $wechat = new \app\api\controller\WechatApi($config);
  90. $expire_time = $config['expires_in'];
  91. if ($expire_time > TIMESTAMP) {
  92. //有效期内
  93. $wechat->access_token_ = $config['access_token'];
  94. } else {
  95. $access_token = $wechat->checkAuth();
  96. $web_expires = TIMESTAMP + 7000; // 提前200秒过期
  97. Db::name('wxconfig')->where(array('id' => $config['id']))->update(array('access_token' => $access_token, 'expires_in' => $web_expires));
  98. }
  99. $return = $wechat->getQRCode($member_info['member_id'], 1);
  100. if ($return) {
  101. $refer_qrcode_weixin = $wechat->getQRUrl($return['ticket']);
  102. if (!is_dir(BASE_UPLOAD_PATH . '/' . ATTACH_INVITER)) {
  103. mkdir(BASE_UPLOAD_PATH . '/' . ATTACH_INVITER, 0755, true);
  104. }
  105. copy($refer_qrcode_weixin, BASE_UPLOAD_PATH . '/' . ATTACH_INVITER . '/' . $member_info['member_id'] . '_weixin.png');
  106. } else {
  107. $refer_qrcode_weixin = '';
  108. $wx_error_msg = $wechat->errMsg;
  109. }
  110. } else {
  111. $refer_qrcode_weixin = UPLOAD_SITE_URL . '/' . ATTACH_INVITER . '/' . $member_info['member_id'] . '_weixin.png';
  112. }
  113. return array(
  114. 'refer_qrcode_weixin' => $refer_qrcode_weixin,
  115. 'wx_error_msg' => $wx_error_msg,
  116. );
  117. }
  118. /**
  119. * 生成URL推荐二维码
  120. * @param type $member_info
  121. * @return type
  122. */
  123. public function qrcode_logo($member_info)
  124. {
  125. !is_dir(BASE_UPLOAD_PATH . '/' . ATTACH_INVITER) && mkdir(BASE_UPLOAD_PATH . '/' . ATTACH_INVITER, 0755, true);
  126. $qrcode_path = BASE_UPLOAD_PATH . '/' . ATTACH_INVITER . '/' . $member_info['member_id'] . '.png';
  127. $refer_qrcode_logo = BASE_UPLOAD_PATH . '/' . ATTACH_INVITER . '/' . $member_info['member_id'] . '_poster.png';
  128. if (!file_exists($qrcode_path)) {
  129. include_once root_path() . 'extend/qrcode/phpqrcode.php';
  130. \QRcode::png(config('ds_config.h5_site_url') . '/pages/home/memberregister/Register?inviter_id=' . $member_info['member_id'], $qrcode_path);
  131. }
  132. $qrcode = imagecreatefromstring(file_get_contents($qrcode_path));
  133. //背景图片
  134. $inviter_back = imagecreatefromstring(file_get_contents(ds_get_pic(ATTACH_COMMON, config('ds_config.inviter_back'))));
  135. $QR_width = imagesx($qrcode);
  136. $QR_height = imagesy($qrcode);
  137. imagecopyresampled($inviter_back, $qrcode, 100, 170, 0, 0, 160, 160, $QR_width, $QR_height);
  138. $portrait = imagecreatefromstring(file_get_contents(get_member_avatar_for_id($member_info['member_id'])));
  139. $QR_width2 = imagesx($portrait);
  140. $QR_height2 = imagesy($portrait);
  141. imagecopyresampled($inviter_back, $portrait, 170, 240, 0, 0, 20, 20, $QR_width2, $QR_height2);
  142. //此处是给图片载入文字
  143. /*
  144. $text = $member_info['member_id'];
  145. $textcolor = imagecolorallocate($inviter_back, 255, 50, 37);
  146. $text_x = 180-(strlen($text)*12)/2;//根据长度定义X轴
  147. imagefttext($inviter_back, 16, 0, $text_x, 145, $textcolor, PUBLIC_PATH . '/font/msyh.ttf', mb_convert_encoding($text, "html-entities", "utf-8"));
  148. */
  149. imagepng($inviter_back, $refer_qrcode_logo);
  150. }
  151. }