WechatUser.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace app\models\user;
  3. use app\models\store\StoreCouponIssue;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\services\WechatService;
  6. use crmeb\traits\ModelTrait;
  7. use crmeb\services\SystemConfigService;
  8. use app\models\routine\RoutineQrcode;
  9. use app\models\store\StoreCouponUser;
  10. use app\models\store\StoreCouponIssueUser;
  11. /**
  12. * TODO 微信用户Model
  13. * Class WechatUser
  14. * @package app\models\user
  15. */
  16. class WechatUser extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'uid';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'wechat_user';
  28. use ModelTrait;
  29. /**
  30. * uid获取小程序Openid
  31. * @param string $uid
  32. * @return bool|mixed
  33. */
  34. public static function getOpenId($uid = '')
  35. {
  36. if ($uid == '') return false;
  37. return self::where('uid', $uid)->value('routine_openid');
  38. }
  39. /**
  40. * TODO 用uid获得openid
  41. * @param $uid
  42. * @param string $openidType
  43. * @return mixed
  44. * @throws \Exception
  45. */
  46. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  47. {
  48. $openid = self::where('uid', $uid)->value($openidType);
  49. return $openid;
  50. }
  51. /**
  52. * TODO 用openid获得uid
  53. * @param $openid
  54. * @param string $openidType
  55. * @return mixed
  56. */
  57. public static function openidTouid($openid, $openidType = 'openid')
  58. {
  59. return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
  60. }
  61. /**
  62. * 小程序创建用户后返回uid
  63. * @param $routineInfo
  64. * @return mixed
  65. */
  66. public static function routineOauth($routine)
  67. {
  68. $routineInfo['nickname'] = filter_emoji($routine['nickName']); //姓名
  69. $routineInfo['sex'] = $routine['gender']; //性别
  70. $routineInfo['language'] = $routine['language']; //语言
  71. $routineInfo['city'] = $routine['city']; //城市
  72. $routineInfo['province'] = $routine['province']; //省份
  73. $routineInfo['country'] = $routine['country']; //国家
  74. $routineInfo['headimgurl'] = $routine['avatarUrl']; //头像
  75. $routineInfo['routine_openid'] = $routine['openId']; //openid
  76. $routineInfo['session_key'] = $routine['session_key']; //会话密匙
  77. $routineInfo['unionid'] = $routine['unionId']; //用户在开放平台的唯一标识符
  78. $routineInfo['user_type'] = 'routine'; //用户类型
  79. $routineInfo['channel'] = $routine['channel'];
  80. $spid = 0; //绑定关系uid
  81. $isByQRScan = false;
  82. //获取是否有扫码进小程序
  83. if ($routine['code']) {
  84. if ($info = RoutineQrcode::getRoutineQrcodeFindType($routine['code'])) {
  85. $spid = $info['third_id'];
  86. $isByQRScan = true;
  87. } else {
  88. $spid = $routine['spid'];
  89. }
  90. } else if ($routine['spid']) {
  91. $spid = $routine['spid'];
  92. }
  93. // 判断unionid 存在根据unionid判断
  94. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
  95. self::edit($routineInfo, $uid, 'uid');
  96. $routineInfo['code'] = $spid;
  97. $routineInfo['isPromoter'] = $isByQRScan;
  98. if ($routine['login_type']) {
  99. $routineInfo['login_type'] = $routine['login_type'];
  100. }
  101. User::updateWechatUser($routineInfo, $uid);
  102. } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
  103. self::edit($routineInfo, $uid, 'uid');
  104. $routineInfo['code'] = $spid;
  105. $routineInfo['isPromoter'] = $isByQRScan;
  106. if ($routine['login_type']) {
  107. $routineInfo['login_type'] = $routine['login_type'];
  108. }
  109. User::updateWechatUser($routineInfo, $uid);
  110. } else {
  111. $routineInfo['add_time'] = time(); //用户添加时间
  112. $routineInfo = self::create($routineInfo);
  113. $res = User::setRoutineUser($routineInfo, $spid);
  114. $uid = $res->uid;
  115. }
  116. return $uid;
  117. }
  118. /**
  119. * 判断是否是小程序用户
  120. * @param int $uid
  121. * @return bool|int|string
  122. */
  123. public static function isRoutineUser($uid = 0)
  124. {
  125. if (!$uid) return false;
  126. return self::where('uid', $uid)->where('user_type', 'routine')->count();
  127. }
  128. /**
  129. * @param int $uid
  130. * @return int
  131. */
  132. public static function isUserStatus($uid = 0)
  133. {
  134. if (!$uid) return 0;
  135. $user = User::getUserInfo($uid);
  136. return $user['status'];
  137. }
  138. /**
  139. * .添加新用户
  140. * @param $openid
  141. * @return object
  142. */
  143. public static function setNewUser($openid)
  144. {
  145. $userInfo = WechatService::getUserInfo($openid);
  146. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  147. exception('请关注公众号!');
  148. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  149. //判断 unionid 是否存在
  150. if (isset($userInfo['unionid'])) {
  151. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  152. unset($userInfo['qr_scene'], $userInfo['qr_scene_str'], $userInfo['qr_scene_str'], $userInfo['subscribe_scene']);
  153. if ($wechatInfo) {
  154. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  155. }
  156. }
  157. self::beginTrans();
  158. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  159. if (!$wechatUser) {
  160. self::rollbackTrans();
  161. exception('用户储存失败!');
  162. }
  163. if (!User::setWechatUser($wechatUser)) {
  164. self::rollbackTrans();
  165. exception('用户信息储存失败!');
  166. }
  167. self::commitTrans();
  168. self::userFirstSubGiveCoupon($openid);
  169. return $wechatUser;
  170. }
  171. /**
  172. * TODO 关注送优惠券
  173. * @param $openid
  174. */
  175. public static function userFirstSubGiveCoupon($openid)
  176. {
  177. $couponIds = StoreCouponIssue::where('status', 1)
  178. ->where('is_give_subscribe', 1)
  179. ->where('is_del', 0)
  180. ->column('id,cid');
  181. if ($couponIds)
  182. foreach ($couponIds as $couponId)
  183. if ($couponId) {
  184. $uid = self::openidToUid($openid);
  185. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  186. StoreCouponIssueUser::addUserIssue($uid, $couponId['id']);
  187. }
  188. }
  189. /**
  190. * 订单金额达到预设金额赠送优惠卷
  191. * @param $uid
  192. */
  193. public static function userTakeOrderGiveCoupon($uid, $total_price)
  194. {
  195. $couponIds = StoreCouponIssue::where('status', 1)
  196. ->where('is_full_give', 1)
  197. ->where('is_del', 0)
  198. ->where('status', 1)
  199. ->where('start_time', '<=', time())
  200. ->where('end_time', '>=', time())
  201. ->column('id,cid,full_reduction,remain_count,is_permanent');
  202. if ($couponIds) {
  203. $couponIssueIds = StoreCouponIssueUser::where('uid', $uid)
  204. ->whereIn('issue_coupon_id', array_column($couponIds, 'id'))
  205. ->column('issue_coupon_id');
  206. foreach ($couponIds as $couponId)
  207. if ($couponId && !in_array($couponId['id'], $couponIssueIds))
  208. if ($total_price >= $couponId['full_reduction'] && (!$couponId['is_permanent'] || $couponId['remain_count'] >= 1)) {
  209. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  210. StoreCouponIssueUser::addUserIssue($uid, $couponId['id']);
  211. StoreCouponIssue::where('id', $couponId['id'])->update(['remain_count' => $couponId['remain_count'] - 1]);
  212. }
  213. }
  214. }
  215. /**
  216. * 更新用户信息
  217. * @param $openid
  218. * @return bool
  219. */
  220. public static function updateUser($openid)
  221. {
  222. $userInfo = WechatService::getUserInfo($openid);
  223. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  224. return self::edit($userInfo->toArray(), $openid, 'openid');
  225. }
  226. /**
  227. * 用户存在就更新 不存在就添加
  228. * @param $openid
  229. */
  230. public static function saveUser($openid)
  231. {
  232. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  233. }
  234. /**
  235. * 用户取消关注
  236. * @param $openid
  237. * @return bool
  238. */
  239. public static function unSubscribe($openid)
  240. {
  241. return self::edit(['subscribe' => 0], $openid, 'openid');
  242. }
  243. /**
  244. * TODO 用uid获得Unionid
  245. * @param $uid
  246. * @return mixed
  247. */
  248. public static function uidToUnionid($uid)
  249. {
  250. return self::where('uid', $uid)->value('unionid');
  251. }
  252. /**
  253. * TODO 获取用户信息
  254. * @param $openid
  255. * @param $openidType
  256. * @return array
  257. * @throws \think\db\exception\DataNotFoundException
  258. * @throws \think\db\exception\ModelNotFoundException
  259. * @throws \think\exception\DbException
  260. */
  261. public static function getWechatInfo($openid, $openidType)
  262. {
  263. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  264. $wechatInfo = self::where($openidType, $openid)->find();
  265. if (!$wechatInfo) {
  266. self::setNewUser($openid);
  267. $wechatInfo = self::where($openidType, $openid)->find();
  268. }
  269. if (!$wechatInfo) exception('获取用户信息失败!');
  270. return $wechatInfo->toArray();
  271. }
  272. }