UserSpread.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * Copyright (c) 2017~2019 http://www.crmeb.com All rights reserved.
  5. * Author: liaofei <136327134@qq.com>
  6. * Date: 2019/3/27 21:44
  7. */
  8. namespace app\models\user;
  9. use app\admin\model\wechat\WechatUser;
  10. use app\models\store\StoreOrder;
  11. use crmeb\services\WechatTemplateService;
  12. use think\Collection;
  13. use think\db\exception\DataNotFoundException;
  14. use think\db\exception\DbException;
  15. use think\db\exception\ModelNotFoundException;
  16. use think\facade\Cache;
  17. use crmeb\traits\ModelTrait;
  18. use crmeb\basic\BaseModel;
  19. use think\facade\Route as Url;
  20. /**
  21. * TODO 用户消费新增金额明细 model
  22. * Class UserBill
  23. * @package app\models\user
  24. */
  25. class UserSpread extends BaseModel
  26. {
  27. /**
  28. * 数据表主键
  29. * @var string
  30. */
  31. protected $pk = 'id';
  32. /**
  33. * 模型名称
  34. * @var string
  35. */
  36. protected $name = 'user_spread';
  37. use ModelTrait;
  38. /**
  39. * @param $uid
  40. * @param $spread_uid
  41. * @return bool|UserSpread
  42. * @throws DataNotFoundException
  43. * @throws DbException
  44. * @throws ModelNotFoundException
  45. */
  46. public static function setSpread($uid, $spread_uid)
  47. {
  48. if (!$spread_uid) {
  49. return true;
  50. }
  51. @file_put_contents('quanju.txt', $spread_uid."-触发用户绑定\r\n", 8);
  52. $sp = $spread_uid;
  53. $userlist = User::column('uid,spread_uid', 'uid');
  54. while ($sp) {
  55. if ($sp == $uid) {
  56. $spread_uid = 0;
  57. break;
  58. }
  59. $sp = $userlist[$sp]['spread_uid'] ?? 0;
  60. }
  61. $user = User::where('uid', $uid)->find();
  62. $spread_user = User::get($spread_uid);
  63. if (!$user || !$spread_user || !$spread_user['is_promoter']) {
  64. return true;
  65. }
  66. //首先获取绑定方式
  67. $bind_type = sys_config('bind_type', 1);
  68. // 通知上级
  69. $openid = WechatUser::where('uid', $spread_uid)->value('openid');
  70. if (!empty($openid)){
  71. $url = Url::buildUrl('/order/detail/wx175446111846779570')->suffix('')->domain(true)->build();
  72. $group = [
  73. 'first' => '下级用户绑定成功通知',
  74. 'remark' => '点击查看订单详情'
  75. ];
  76. // $group = array_merge($group, [
  77. // 'thing4' => 'cs', //商品名称
  78. //// 'character_string2' => $order['pay_type'] == 'offline' ? '线下支付' : date('Y/m/d H:i', $order['pay_time']),
  79. //// 'phone_number21' => $order['user_address'],
  80. //// 'phone_number21' => '15356590576',
  81. // 'thing13' => 'cs', //快递公司
  82. // 'character_string14' => 12345789//快递单号
  83. // ]);
  84. // @file_put_contents('quanju.txt', json_encode($group)."-模版消息1\r\n", 8);
  85. // $res= WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_POSTAGE_SUCCESS, $group, $url);
  86. // @file_put_contents('quanju.txt', json_encode($res)."-模版消息返回1\r\n", 8);
  87. $group = array_merge($group, [
  88. // 'thing1' => $user['nickname'], //用户姓名
  89. // 'character_string2' => $order['pay_type'] == 'offline' ? '线下支付' : date('Y/m/d H:i', $order['pay_time']),
  90. // 'phone_number21' => $order['user_address'],
  91. // 'phone_number21' => '15356590576',
  92. 'character_string7' => $user['account'], //用户账号
  93. 'time3' => time(), //绑定时间
  94. ]);
  95. @file_put_contents('quanju.txt', $openid."-openid\r\n", 8);
  96. @file_put_contents('quanju.txt', json_encode($group)."-模版消息3\r\n", 8);
  97. $res= WechatTemplateService::sendTemplate($openid, WechatTemplateService::BARGAIN_SUCCESS, $group);
  98. @file_put_contents('quanju.txt', json_encode($res)."-模版消息返回3\r\n", 8);
  99. }
  100. switch ($bind_type) {
  101. case 2:
  102. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  103. return true;
  104. }
  105. return self::create([
  106. 'uid' => $uid,
  107. 'spread_uid' => $spread_uid,
  108. 'lock' => 0,
  109. 'spread_time' => time()
  110. ]);
  111. break;
  112. case 3:
  113. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  114. return true;
  115. }
  116. self::create([
  117. 'uid' => $uid,
  118. 'spread_uid' => $spread_uid,
  119. 'lock' => 0,
  120. 'spread_time' => time()
  121. ]);
  122. $user['spread_uid'] = $spread_uid;
  123. $user['spread_time'] = time();
  124. return $user->save() && User::setUserSpreadCount($spread_uid);
  125. break;
  126. default:
  127. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  128. return true;
  129. }
  130. self::create([
  131. 'uid' => $uid,
  132. 'spread_uid' => $spread_uid,
  133. 'lock' => 1,
  134. 'spread_time' => time()
  135. ]);
  136. $user['spread_uid'] = $spread_uid;
  137. $user['spread_time'] = time();
  138. return $user->save() && User::setUserSpreadCount($spread_uid);
  139. break;
  140. }
  141. }
  142. /**
  143. * @param $uid
  144. * @return bool
  145. * @throws DataNotFoundException
  146. * @throws DbException
  147. * @throws ModelNotFoundException
  148. */
  149. public static function setSpreadSure($uid)
  150. {
  151. //首先获取绑定方式
  152. $bind_type = sys_config('bind_type', 1);
  153. if ($bind_type != 2) {
  154. return true;
  155. }
  156. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  157. return true;
  158. }
  159. $log = self::where('uid', $uid)->order('spread_time', 'desc')->find();
  160. if (!$log) {
  161. return true;
  162. }
  163. $sp = $log['spread_uid'];
  164. $userlist = User::column('uid,spread_uid', 'uid');
  165. while ($sp) {
  166. if ($sp == $uid) {
  167. $log['spread_uid'] = 0;
  168. break;
  169. }
  170. $sp = $userlist[$sp]['spread_uid'];
  171. }
  172. if (!$log['spread_uid']) {
  173. return true;
  174. }
  175. $log['lock'] = 1;
  176. return $log->save() && User::where('uid', $uid)->update(['spread_uid' => $log['spread_uid'], 'spread_time' => $log['spread_time']]) && User::setUserSpreadCount($log['spread_uid']);
  177. }
  178. }