Account.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. namespace app\abutment\logic;
  3. use app\abutment\model\SupplierUser as SupplierUserModel;
  4. use app\abutment\model\SupplierRelationUser as SupplierRelationUserModel;
  5. use app\abutment\model\SupplierUser;
  6. use think\Exception;
  7. use think\facade\Cache;
  8. use think\facade\Config;
  9. use think\facade\Db;
  10. use think\helper\Str;
  11. //供应商账号管理
  12. class Account
  13. {
  14. //登录
  15. public static function login(array $param = [])
  16. {
  17. $db = new SupplierUserModel();
  18. $res = $db->where([
  19. 'is_del' => $db::$is_del_normal,
  20. 'mobile' => $param['mobile']
  21. ])
  22. ->findOrEmpty()
  23. ->toArray();
  24. if (empty($res)) return json_show(1004, '该账号不存在');
  25. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  26. $password = get_encryption_password($param['password'], $res['salt']);
  27. if ($password['password'] != $res['password']) return json_show(1004, '密码错误');
  28. //更新token
  29. $token = Str::random(40, -1);
  30. $expire_int = mt_rand(3600, 7200);
  31. $expire_time = date('Y-m-d H:i:s', time() + $expire_int);
  32. $rs = $db
  33. ->where($db->getPk(), $res[$db->getPk()])
  34. ->save(['token' => $token, 'expire_time' => $expire_time]);
  35. if (!$rs) throw new Exception('更新账号token信息失败');
  36. $list = SupplierRelationUserModel::field('id,supplierNo,supplierName')
  37. ->where([
  38. 'is_del' => $db::$is_del_normal,
  39. 'uid' => $res['uid'],
  40. ])
  41. ->select()
  42. ->toArray();
  43. $info = [
  44. 'uid' => $res['uid'],
  45. 'nickname' => $res['nickname'],
  46. 'mobile' => $res['mobile'],
  47. 'email' => $res['email'],
  48. 'token' => $token,
  49. 'expire_time' => $expire_time,
  50. 'supplier_list' => $list,
  51. ];
  52. //过期时间指定一个具体的日期的时候,还没到这个日期缓存就过期了,指定一个秒数,可以符合预期,邪门儿
  53. Cache::set(Config::get('redis_key.user_info_token') . $token, json_encode($info), $expire_int);
  54. return json_show(0, '登录成功', $info);
  55. }
  56. //获取供应商账号列表
  57. public static function getAccountList(array $param = [])
  58. {
  59. $db = new SupplierUserModel();
  60. $rs = $db
  61. ->where('is_del', $db::$is_del_normal);
  62. if ($param['keyword'] != '') $rs->whereLike('nickname|mobile', '%' . $param['keyword'] . '%');
  63. if ($param['status'] != '') $rs->where('status', $param['status']);
  64. if ($param['supplierNo'] != '') {
  65. $uids = SupplierRelationUserModel::where([
  66. 'is_del' => $db::$is_del_normal,
  67. 'supplierNo' => $param['supplierNo']
  68. ])->column('uid');
  69. $rs->whereIn('uid', $uids);
  70. }
  71. $count = $rs->count('uid');
  72. $list = $rs
  73. ->field('uid,nickname,mobile,email,status,addtime,creater')
  74. ->order('addtime', 'desc')
  75. ->page($param['page'], $param['size'])
  76. ->append(['supplier_list'])
  77. ->withAttr('supplier_list', function ($val, $data) use ($db) {
  78. return SupplierRelationUserModel::where([
  79. 'is_del' => $db::$is_del_normal,
  80. 'uid' => $data['uid']
  81. ])->field('supplierNo,supplierName,status')
  82. ->select()
  83. ->toArray();
  84. })
  85. ->select()
  86. ->toArray();
  87. return json_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  88. }
  89. //修改供应商账号密码
  90. public static function changePassword(array $param = [])
  91. {
  92. $db = new SupplierUserModel();
  93. $res = $db
  94. ->where([
  95. 'uid' => $param['uid'],
  96. 'is_del' => $db::$is_del_normal,
  97. ])
  98. ->field('uid,password,salt,status,is_del')
  99. ->findOrEmpty()
  100. ->toArray();
  101. if (empty($res)) return json_show(1004, '该账号不存在');
  102. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  103. $old_password = get_encryption_password($param['old_password'], $res['salt']);
  104. if ($old_password['password'] != $res['password']) return json_show(1004, '密码错误');
  105. //更新密码
  106. $password = get_encryption_password($param['new_password']);
  107. $rs = $db
  108. ->where('uid', $res['uid'])
  109. ->save(['password' => $password['password'], 'salt' => $password['salt']]);
  110. return $rs ? json_show(0, '修改密码成功') : json_show(1005, '修改密码失败');
  111. }
  112. //获取供应商账号账号信息
  113. public static function readAccount(array $param = [])
  114. {
  115. $res = SupplierUser::field('uid,nickname,mobile,email')
  116. ->where([
  117. 'uid' => $param['uid'],
  118. 'is_del' => SupplierUser::$is_del_normal,
  119. ])
  120. ->append(['supplier_list'])
  121. ->withAttr('supplier_list', function ($val, $data) {
  122. return SupplierRelationUserModel::field('id,supplierNo,supplierName,status,addtime')
  123. ->where([
  124. 'is_del' => SupplierUser::$is_del_normal,
  125. 'uid' => $data['uid']
  126. ])
  127. ->select()
  128. ->toArray();
  129. })
  130. ->findOrEmpty()
  131. ->toArray();
  132. return json_show(0, '获取账号信息成功', $res);
  133. }
  134. //添加供应商账号
  135. public static function addAccount(array $param = [], string $token = '')
  136. {
  137. $user = GetUserInfo($token);
  138. //查找供应商名称
  139. $supplierName = Db::name('supplier')
  140. ->whereIn('code', $param['supplierNo'])
  141. ->column('name', 'code');
  142. if (empty($supplierName)) return json_show(1004, '供应商不存在');
  143. Db::connect('mysql_sys')->startTrans();
  144. try {
  145. $db = new SupplierUserModel();
  146. $res = $db
  147. ->field('uid,status')
  148. ->where([
  149. 'is_del' => $db::$is_del_normal,
  150. 'mobile' => $param['mobile'],
  151. ])->findOrEmpty();
  152. if ($res->isEmpty()) {
  153. //新增账号
  154. $password = get_encryption_password(Config::get('app.default_password'));
  155. $res->uid = $db->insertGetId([
  156. 'nickname' => $param['nickname'],//姓名
  157. 'mobile' => $param['mobile'],//手机号
  158. 'email' => $param['email'],//邮箱
  159. 'password' => $password['password'],//密码密文
  160. 'salt' => $password['salt'],//盐值
  161. 'status' => $db::$status_normal,
  162. 'is_del' => $db::$is_del_normal,
  163. 'createrid' => $user['data']['user_id'] ?? 0,
  164. 'creater' => $user['data']['nickname'] ?? '',
  165. ]);
  166. $res->status = $db::$status_normal;
  167. }
  168. $relation = SupplierRelationUserModel::where([
  169. 'is_del' => $db::$is_del_normal,
  170. 'uid' => $res->uid
  171. ])->whereIn('supplierNo', $param['supplierNo'])
  172. ->column('id', 'supplierNo');
  173. $insert_data = [];
  174. foreach ($param['supplierNo'] as $supplierNo) {
  175. if (isset($relation[$supplierNo])) continue;
  176. else {
  177. $insert_data[] = [
  178. 'uid' => $res->uid,
  179. 'supplierNo' => $supplierNo,
  180. 'supplierName' => $supplierName[$supplierNo] ?? '',
  181. 'status' => $res->status,
  182. 'is_del' => $db::$is_del_normal,
  183. 'createrid' => $user['data']['user_id'] ?? 0,
  184. 'creater' => $user['data']['nickname'] ?? '',
  185. ];
  186. }
  187. }
  188. if ($insert_data) Db::connect('mysql_sys')->name('supplier_relation_user')->insertAll($insert_data);
  189. Db::connect('mysql_sys')->commit();
  190. return json_show(0, '操作成功');
  191. } catch (Exception $exception) {
  192. Db::connect('mysql_sys')->rollback();
  193. return json_show(1005, '操作失败' . $exception->getMessage());
  194. }
  195. }
  196. //修改供应商账号
  197. public static function editAccount(array $param = [])
  198. {
  199. $db = new SupplierUserModel();
  200. $res = $db
  201. ->field('uid')
  202. ->where(['uid' => $param['uid'], 'is_del' => $db::$is_del_normal])
  203. ->findOrEmpty()
  204. ->isEmpty();
  205. if ($res) return json_show(1004, '该账号不存在');
  206. $rs = $db
  207. ->where('uid', $param['uid'])
  208. ->save($param);
  209. return $rs ? json_show(0, '修改成功') : json_show(1005, '修改失败');
  210. }
  211. //修改供应商账号状态
  212. public static function statusAccount(array $param = [])
  213. {
  214. Db::connect('mysql_sys')->startTrans();
  215. try {
  216. $db = new SupplierUserModel();
  217. $res = $db
  218. ->field('uid,status')
  219. ->where([
  220. 'uid' => $param['uid'],
  221. 'is_del' => $db::$is_del_normal,
  222. ])->findOrEmpty();
  223. if ($res->isEmpty()) throw new Exception('该账号不存在');
  224. if ($res->status == $param['status']) throw new Exception('不能重复操作');
  225. $db->where([
  226. 'uid' => $param['uid'],
  227. 'is_del' => $db::$is_del_normal,
  228. ])->where('status', '<>', $param['status'])
  229. ->save(['status' => $param['status']]);
  230. SupplierRelationUserModel::where([
  231. 'uid' => $param['uid'],
  232. 'is_del' => $db::$is_del_normal,
  233. ])->where('status', '<>', $param['status'])
  234. ->save(['status' => $param['status']]);
  235. Db::connect('mysql_sys')->commit();
  236. return json_show(0, '操作成功');
  237. } catch (Exception $exception) {
  238. Db::connect('mysql_sys')->rollback();
  239. return json_show(1005, '操作失败,' . $exception->getMessage());
  240. }
  241. }
  242. //删除供应商账号
  243. public static function deleteAccount(int $uid = 0)
  244. {
  245. Db::connect('mysql_sys')->startTrans();
  246. try {
  247. $db = new SupplierUserModel();
  248. $res = $db
  249. ->field('uid')
  250. ->where([
  251. 'uid' => $uid,
  252. 'is_del' => $db::$is_del_normal,
  253. ])->findOrEmpty();
  254. if ($res->isEmpty()) throw new Exception('该账号不存在或已删除');
  255. $db->where('uid', $uid)
  256. ->where('is_del', $db::$is_del_normal)
  257. ->save(['is_del' => $db::$is_del_deleted]);
  258. SupplierRelationUserModel::where('uid', $uid)
  259. ->where('is_del', $db::$is_del_normal)
  260. ->save(['is_del' => $db::$is_del_deleted]);
  261. Db::connect('mysql_sys')->commit();
  262. return json_show(0, '删除成功');
  263. } catch (Exception $exception) {
  264. Db::connect('mysql_sys')->rollback();
  265. return json_show(1005, '删除失败,' . $exception->getMessage());
  266. }
  267. }
  268. }