UserController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizMall\message\services\SmsService;
  7. use bizMall\user\classes\UserClass;
  8. use bizMall\user\classes\UserGrowthClass;
  9. use bizMall\user\classes\UserIntegralClass;
  10. use bizMall\user\services\UserAssetService;
  11. use bizMall\user\services\UserService;
  12. use biz\shop\classes\ShopClass;
  13. use common\components\dict;
  14. use common\components\imgUtil;
  15. use common\components\jwt;
  16. use common\components\noticeUtil;
  17. use common\components\stringUtil;
  18. use Yii;
  19. use common\components\util;
  20. use yii\helpers\Json;
  21. use Overtrue\ChineseCalendar\Calendar;
  22. class UserController extends BaseController
  23. {
  24. //二维码收款使用 ssh
  25. public $guestAccess = ['discount', 'mini-mobile', 'create', 'account-login', 'current-info'];
  26. //清空登录信息 ssh 20250215
  27. public function actionClearLogin()
  28. {
  29. $user = $this->user;
  30. $user->miniOpenId = '';
  31. $user->save();
  32. util::complete();
  33. }
  34. //修改信息 ssh 20221015
  35. public function actionUpdate()
  36. {
  37. $post = Yii::$app->request->post();
  38. $user = $this->user;
  39. if (empty($user)) {
  40. util::fail('请先登录');
  41. }
  42. $userId = $user->id ?? 0;
  43. $address = $post['address'] ?? '';
  44. $floor = $post['floor'] ?? '';
  45. $fullAddress = $address . $floor;
  46. $post['fullAddress'] = $fullAddress;
  47. UserClass::updateUserInfo($user, $post);
  48. util::complete();
  49. }
  50. //修改生日 ssh 20250830
  51. public function actionModifyBirthday()
  52. {
  53. date_default_timezone_set('PRC');
  54. $get = Yii::$app->request->get();
  55. $lunar = $get['lunar'] ?? 0;
  56. $birthdayMonth = $get['birthdayMonth'] ?? 1;
  57. if ($birthdayMonth > 12 || $birthdayMonth < 1) {
  58. util::fail('月份必须1到12月');
  59. }
  60. $birthdayDate = $get['birthdayDate'] ?? 1;
  61. if ($birthdayDate > 31 || $birthdayDate < 1) {
  62. util::fail('日期必须1到31号');
  63. }
  64. if ($lunar == 1 && $birthdayDate == 31) {
  65. util::fail('农历没有31号');
  66. }
  67. $user = $this->user;
  68. if ($user->birthdaySet == 1) {
  69. util::fail('你已经设置过了');
  70. }
  71. if ($lunar == 1) {
  72. try {
  73. //农历转阳历,有多处使用,需要同步修改,关键词 change_my_birthday
  74. $calendar = new Calendar();
  75. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  76. $month = $result['gregorian_month'] ?? 1;
  77. $date = $result['gregorian_day'] ?? 1;
  78. $birthday = date("Y") . '-' . $month . '-' . $date;
  79. } catch (\InvalidArgumentException $e) {
  80. $msg = $e->getMessage();
  81. $birthday = '0000-00-00 00:00:00';
  82. util::fail($msg);
  83. }
  84. } else {
  85. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  86. }
  87. $birthdayTime = strtotime($birthday);
  88. $user->lunar = $lunar;
  89. $user->birthdayDate = $birthdayDate;
  90. $user->birthdayMonth = $birthdayMonth;
  91. $user->birthday = $birthday;
  92. $user->birthdayTime = $birthdayTime;
  93. $user->birthdaySet = 1;
  94. $user->save();
  95. $userId = $user->id;
  96. $customList = \bizMall\custom\classes\CustomClass::getAllByCondition(['userId' => $userId], null, '*', null, true);
  97. if (!empty($customList)) {
  98. foreach ($customList as $custom) {
  99. $custom->lunar = $lunar;
  100. $custom->birthdayDate = $birthdayDate;
  101. $custom->birthdayMonth = $birthdayMonth;
  102. $custom->birthday = $birthday;
  103. $custom->birthdayTime = $birthdayTime;
  104. $custom->save();
  105. }
  106. }
  107. util::complete('修改成功');
  108. }
  109. //获取当前登录用户的信息 ssh 20221014
  110. public function actionCurrentInfo()
  111. {
  112. $user = $this->user;
  113. if (!empty($user)) {
  114. $birthday = $user->birthday ?? '';
  115. //如果当年的生日时间没有更新,则需要给更新
  116. $year = date("Y", strtotime($birthday));
  117. $thisYear = date("Y");
  118. if ($year != $thisYear) {
  119. $lunar = $user->lunar ?? 0;
  120. $userId = $this->userId;
  121. $birthdayMonth = $user->birthdayMonth ?? 0;
  122. $birthdayDate = $user->birthdayDate ?? 0;
  123. if ($lunar == 1 && $birthdayMonth > 0 && $birthdayDate > 0) {
  124. try {
  125. //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
  126. $calendar = new Calendar();
  127. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  128. $month = $result['gregorian_month'] ?? 1;
  129. $date = $result['gregorian_day'] ?? 1;
  130. $birthday = date("Y") . '-' . $month . '-' . $date;
  131. $user->birthday = $birthday;
  132. $user->save();
  133. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  134. } catch (\InvalidArgumentException $e) {
  135. $msg = $e->getMessage();
  136. noticeUtil::push("更新生日出错了呢,userId:" . $userId . ' ' . $msg, '15280215347');
  137. }
  138. } else {
  139. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  140. $user->birthday = $birthday;
  141. $user->save();
  142. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  143. }
  144. }
  145. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  146. $user = $user->toArray();
  147. $shortAvatar = $user['avatar'] ?? '';
  148. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  149. $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  150. $user['avatar'] = $avatar;
  151. $user['smallAvatar'] = $smallAvatar;
  152. $user['shortAvatar'] = $shortAvatar;
  153. }
  154. // 补充店铺信息
  155. $shop = [];
  156. if (!empty($this->shop)) {
  157. $intraCityRet = ShopClass::hasIntraCity($this->shop);
  158. $shop['openIntraCity'] = $intraCityRet['openIntraCity'];
  159. $shop['hcFreeKm'] = $intraCityRet['hcFreeKm'];
  160. $shop['hcMap'] = $intraCityRet['hcMap'];
  161. }
  162. util::success(['info' => $user, 'shop' => $shop]);
  163. }
  164. //使用账号登录 ssh 20220920
  165. public function actionAccountLogin()
  166. {
  167. $getParams = Yii::$app->request->get();
  168. $postParams = Yii::$app->request->post();
  169. $allParams = array_merge($getParams, $postParams);
  170. $mobile = $allParams['mobile'] ?? '';
  171. $password = $allParams['password'] ?? '';
  172. if ($password != 'a.1234') {
  173. util::fail('密码错误');
  174. }
  175. $user = UserClass::getByCondition(['mobile' => $mobile], true);
  176. if (empty($user)) {
  177. util::fail('账号无效');
  178. }
  179. $userId = $user->id ?? 0;
  180. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  181. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  182. $user = $user->toArray();
  183. $user['hasMap'] = dict::getDict('hasMap');
  184. util::success(['user' => $user, 'token' => $token]);
  185. }
  186. //生成用户和花店增加客户 ssh 20250307
  187. public function actionCreate()
  188. {
  189. $post = Yii::$app->request->post();
  190. $user = UserClass::replaceUser($post);
  191. if (!empty($this->shop)) {
  192. //花店增加客户
  193. CustomClass::buildRelation($this->shop, $user);
  194. }
  195. $userId = $user->id ?? 0;
  196. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  197. $fullUser = UserClass::getById($userId, true);
  198. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  199. $fullUser = $fullUser->toArray();
  200. $fullUser['hasMap'] = dict::getDict('hasMap');
  201. util::success(['user' => $fullUser, 'token' => $token]);
  202. }
  203. //最近店铺 ssh 2021.2.27
  204. public function actionRecentShop()
  205. {
  206. $list = [];
  207. //显示国兰的信息 0不是 1是
  208. $showGlInfo = getenv('SHOW_GL_INFO');
  209. $info = $showGlInfo === false ? 1 : $showGlInfo;
  210. util::success(['list' => $list, 'showGlInfo' => $info]);
  211. }
  212. //获取登陆客户的资产 ssh 2019.11.22
  213. public function actionAsset()
  214. {
  215. $asset = UserAssetService::getByUserId($this->userId);
  216. util::success($asset);
  217. }
  218. //获取登陆客户的优惠情况 ssh 2019.12.3
  219. public function actionDiscount()
  220. {
  221. $discount = UserAssetService::getDiscount($this->userId);
  222. util::success($discount);
  223. }
  224. //小程序用户完整信息 ssh 2019.12.12
  225. public function actionMiniFullInfo()
  226. {
  227. $post = Yii::$app->request->post();
  228. $iv = $post['iv'] ?? '';
  229. $encryptedData = $post['encryptedData'] ?? '';
  230. $merchant = WxOpenClass::getMallWxMiniBase();
  231. $appId = $merchant['miniAppId'];
  232. $miniOpenId = $post['miniOpenId'];
  233. if (empty($miniOpenId)) {
  234. util::success(['hasNoOpenId' => 1], '登录失败');
  235. }
  236. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  237. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  238. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  239. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  240. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  241. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  242. if ($errCode != 0) {
  243. Yii::info($result . ' ' . $errCode);
  244. util::fail('获取用户信息失败');
  245. }
  246. Yii::info('小程序获取用户信息:' . $result);
  247. $originalInfo = Json::decode($result);
  248. util::success($originalInfo);
  249. }
  250. //小程序用户手机号
  251. public function actionMiniMobile()
  252. {
  253. $post = Yii::$app->request->post();
  254. $iv = $post['iv'];
  255. $encryptedData = $post['encryptedData'];
  256. $merchant = WxOpenClass::getMallWxMiniBase();
  257. $appId = $merchant['miniAppId'];
  258. $miniOpenId = $post['miniOpenId'];
  259. if (empty($miniOpenId)) {
  260. util::success(['hasNoOpenId' => 1], '登录失败');
  261. }
  262. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  263. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  264. if (empty($sessionKey)) {
  265. util::fail('系统错误,session empty');
  266. }
  267. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  268. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  269. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  270. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  271. if ($errCode != 0) {
  272. util::fail('解密失败');
  273. }
  274. $arr = Json::decode($result);
  275. $mobile = $arr['purePhoneNumber'] ?? '';
  276. if (empty($mobile)) {
  277. util::fail('没有取到手机号,请重试');
  278. }
  279. util::success(['mobile' => $mobile]);
  280. }
  281. //密码修改
  282. public function actionPassword()
  283. {
  284. $post = Yii::$app->request->post();
  285. $old = $post['old'] ?? '';
  286. $new = $post['new'] ?? '';
  287. $confirm = $post['confirm'] ?? '';
  288. if (empty($new)) {
  289. util::fail('请输入新密码');
  290. }
  291. if ($new != $confirm) {
  292. util::fail('二次密码不一致');
  293. }
  294. $userId = $this->userId;
  295. $user = UserService::getUserInfo($userId);
  296. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  297. if (empty($old)) {
  298. util::fail('请填写旧密码');
  299. }
  300. if (!password_verify($old, $user['payPassword'])) {
  301. util::fail('旧密码错误');
  302. }
  303. }
  304. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  305. util::complete('修改成功');
  306. }
  307. //申请会员 ssh 2019.12.18
  308. public function actionApplyMember()
  309. {
  310. $post = Yii::$app->request->post();
  311. $mobile = $post['mobile'] ?? 0;
  312. if (!stringUtil::isMobile($mobile)) {
  313. util::fail('请输入手机号');
  314. }
  315. $userId = $this->userId;
  316. $asset = UserAssetService::getByUserId($userId);
  317. if (isset($asset['member']) && $asset['member'] != -1) {
  318. util::fail('已经是会员了');
  319. }
  320. $code = $post['code'] ?? '';
  321. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  322. if (empty($cacheCode) || $cacheCode != $code) {
  323. util::fail('验证码错误');
  324. }
  325. $update = [];
  326. if (!empty($post['realName'])) {
  327. $update['realName'] = $post['realName'];
  328. }
  329. if (!empty($post['birthday'])) {
  330. $update['birthday'] = $post['birthday'];
  331. }
  332. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  333. if ($userId == $user['id']) {
  334. util::fail('手机号已验证');
  335. }
  336. if (!empty($user)) {
  337. util::fail('手机号已经注册过了');
  338. }
  339. $update['mobile'] = $mobile;
  340. $userInfo = $this->user->attributes ?? [];
  341. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  342. util::complete('注册成功');
  343. }
  344. //登陆客户的信息 ssh 2020.3.12
  345. public function actionLoginDetail()
  346. {
  347. $user = $this->user->attributes ?? [];
  348. util::success($user);
  349. }
  350. //成长值列表
  351. public function actionGrowthList()
  352. {
  353. $where = [];
  354. $where['shopId'] = $this->shopId;
  355. $where['userId'] = $this->userId;
  356. $list = UserGrowthClass::getGrowthList($where);
  357. util::success($list);
  358. }
  359. //积分列表
  360. public function actionIntegralList()
  361. {
  362. $where = [];
  363. $where['shopId'] = $this->shopId;
  364. $where['userId'] = $this->userId;
  365. $list = UserIntegralClass::getIntegralList($where);
  366. util::success($list);
  367. }
  368. }