WechatUser.php 10 KB

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