UserController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. if ($userId > 0) {
  181. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  182. if ($mallUpgrading == 1) {
  183. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  184. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  185. if (!in_array($userId, $allowShopAdminIds)) {
  186. util::fail('系统升级中,稍后再试,编号:' . $userId);
  187. }
  188. }
  189. }
  190. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  191. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  192. $user = $user->toArray();
  193. $user['hasMap'] = dict::getDict('hasMap');
  194. util::success(['user' => $user, 'token' => $token]);
  195. }
  196. //生成用户和花店增加客户 ssh 20250307
  197. public function actionCreate()
  198. {
  199. $post = Yii::$app->request->post();
  200. $user = UserClass::replaceUser($post);
  201. if (!empty($this->shop)) {
  202. //花店增加客户
  203. CustomClass::buildRelation($this->shop, $user);
  204. }
  205. $userId = $user->id ?? 0;
  206. if ($userId > 0) {
  207. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  208. if ($mallUpgrading == 1) {
  209. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  210. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  211. if (!in_array($userId, $allowShopAdminIds)) {
  212. util::fail('系统升级中,稍后再试,编号:' . $userId);
  213. }
  214. }
  215. }
  216. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  217. $fullUser = UserClass::getById($userId, true);
  218. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  219. $fullUser = $fullUser->toArray();
  220. $fullUser['hasMap'] = dict::getDict('hasMap');
  221. util::success(['user' => $fullUser, 'token' => $token]);
  222. }
  223. //最近店铺 ssh 2021.2.27
  224. public function actionRecentShop()
  225. {
  226. $list = [];
  227. //显示国兰的信息 0不是 1是
  228. $showGlInfo = getenv('SHOW_GL_INFO');
  229. $info = $showGlInfo === false ? 1 : $showGlInfo;
  230. util::success(['list' => $list, 'showGlInfo' => $info]);
  231. }
  232. //获取登陆客户的资产 ssh 2019.11.22
  233. public function actionAsset()
  234. {
  235. $asset = UserAssetService::getByUserId($this->userId);
  236. util::success($asset);
  237. }
  238. //获取登陆客户的优惠情况 ssh 2019.12.3
  239. public function actionDiscount()
  240. {
  241. $discount = UserAssetService::getDiscount($this->userId);
  242. util::success($discount);
  243. }
  244. //小程序用户完整信息 ssh 2019.12.12
  245. public function actionMiniFullInfo()
  246. {
  247. $post = Yii::$app->request->post();
  248. $iv = $post['iv'] ?? '';
  249. $encryptedData = $post['encryptedData'] ?? '';
  250. $merchant = WxOpenClass::getMallWxMiniBase();
  251. $appId = $merchant['miniAppId'];
  252. $miniOpenId = $post['miniOpenId'];
  253. if (empty($miniOpenId)) {
  254. util::success(['hasNoOpenId' => 1], '登录失败');
  255. }
  256. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  257. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  258. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  259. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  260. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  261. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  262. if ($errCode != 0) {
  263. Yii::info($result . ' ' . $errCode);
  264. util::fail('获取用户信息失败');
  265. }
  266. Yii::info('小程序获取用户信息:' . $result);
  267. $originalInfo = Json::decode($result);
  268. util::success($originalInfo);
  269. }
  270. //小程序用户手机号
  271. public function actionMiniMobile()
  272. {
  273. $post = Yii::$app->request->post();
  274. $iv = $post['iv'];
  275. $encryptedData = $post['encryptedData'];
  276. $merchant = WxOpenClass::getMallWxMiniBase();
  277. $appId = $merchant['miniAppId'];
  278. $miniOpenId = $post['miniOpenId'];
  279. if (empty($miniOpenId)) {
  280. util::success(['hasNoOpenId' => 1], '登录失败');
  281. }
  282. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  283. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  284. if (empty($sessionKey)) {
  285. util::fail('系统错误,session empty');
  286. }
  287. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  288. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  289. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  290. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  291. if ($errCode != 0) {
  292. util::fail('解密失败');
  293. }
  294. $arr = Json::decode($result);
  295. $mobile = $arr['purePhoneNumber'] ?? '';
  296. if (empty($mobile)) {
  297. util::fail('没有取到手机号,请重试');
  298. }
  299. util::success(['mobile' => $mobile]);
  300. }
  301. //密码修改
  302. public function actionPassword()
  303. {
  304. $post = Yii::$app->request->post();
  305. $old = $post['old'] ?? '';
  306. $new = $post['new'] ?? '';
  307. $confirm = $post['confirm'] ?? '';
  308. if (empty($new)) {
  309. util::fail('请输入新密码');
  310. }
  311. if ($new != $confirm) {
  312. util::fail('二次密码不一致');
  313. }
  314. $userId = $this->userId;
  315. $user = UserService::getUserInfo($userId);
  316. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  317. if (empty($old)) {
  318. util::fail('请填写旧密码');
  319. }
  320. if (!password_verify($old, $user['payPassword'])) {
  321. util::fail('旧密码错误');
  322. }
  323. }
  324. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  325. util::complete('修改成功');
  326. }
  327. //申请会员 ssh 2019.12.18
  328. public function actionApplyMember()
  329. {
  330. $post = Yii::$app->request->post();
  331. $mobile = $post['mobile'] ?? 0;
  332. if (!stringUtil::isMobile($mobile)) {
  333. util::fail('请输入手机号');
  334. }
  335. $userId = $this->userId;
  336. $asset = UserAssetService::getByUserId($userId);
  337. if (isset($asset['member']) && $asset['member'] != -1) {
  338. util::fail('已经是会员了');
  339. }
  340. $code = $post['code'] ?? '';
  341. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  342. if (empty($cacheCode) || $cacheCode != $code) {
  343. util::fail('验证码错误');
  344. }
  345. $update = [];
  346. if (!empty($post['realName'])) {
  347. $update['realName'] = $post['realName'];
  348. }
  349. if (!empty($post['birthday'])) {
  350. $update['birthday'] = $post['birthday'];
  351. }
  352. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  353. if ($userId == $user['id']) {
  354. util::fail('手机号已验证');
  355. }
  356. if (!empty($user)) {
  357. util::fail('手机号已经注册过了');
  358. }
  359. $update['mobile'] = $mobile;
  360. $userInfo = $this->user->attributes ?? [];
  361. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  362. util::complete('注册成功');
  363. }
  364. //登陆客户的信息 ssh 2020.3.12
  365. public function actionLoginDetail()
  366. {
  367. $user = $this->user->attributes ?? [];
  368. util::success($user);
  369. }
  370. //成长值列表
  371. public function actionGrowthList()
  372. {
  373. $where = [];
  374. $where['shopId'] = $this->shopId;
  375. $where['userId'] = $this->userId;
  376. $list = UserGrowthClass::getGrowthList($where);
  377. util::success($list);
  378. }
  379. //积分列表
  380. public function actionIntegralList()
  381. {
  382. $where = [];
  383. $where['shopId'] = $this->shopId;
  384. $where['userId'] = $this->userId;
  385. $list = UserIntegralClass::getIntegralList($where);
  386. util::success($list);
  387. }
  388. }