Account.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. ->where(['code' => $param['supplierNo']])
  141. ->value('name', '');
  142. if ($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. $is_insert = true;
  153. if ($res->isEmpty()) {
  154. //新增账号
  155. $password = get_encryption_password(Config::get('app.default_password'));
  156. $res->uid = $db->insertGetId([
  157. 'nickname' => $param['nickname'],//姓名
  158. 'mobile' => $param['mobile'],//手机号
  159. 'email' => $param['email'],//邮箱
  160. 'password' => $password['password'],//密码密文
  161. 'salt' => $password['salt'],//盐值
  162. 'status' => $db::$status_normal,
  163. 'is_del' => $db::$is_del_normal,
  164. 'createrid' => $user['data']['user_id'] ?? 0,
  165. 'creater' => $user['data']['nickname'] ?? '',
  166. ]);
  167. $res->status = $db::$status_normal;
  168. } else {
  169. $rs = SupplierRelationUserModel::field('id')
  170. ->where([
  171. 'is_del' => $db::$is_del_normal,
  172. 'uid' => $res->uid,
  173. 'supplierNo' => $param['supplierNo'],
  174. ])->findOrEmpty()
  175. ->isEmpty();
  176. if ($rs) throw new Exception('不能重复添加');//该关联关系已存在,无需新增
  177. else $is_insert = false;
  178. }
  179. if ($is_insert) {
  180. SupplierRelationUserModel::create([
  181. 'uid' => $res->uid,
  182. 'supplierNo' => $param['supplierNo'],
  183. 'supplierName' => $supplierName,
  184. 'status' => $res->status,
  185. 'is_del' => $db::$is_del_normal,
  186. 'createrid' => $user['data']['user_id'] ?? 0,
  187. 'creater' => $user['data']['nickname'] ?? '',
  188. ])->save();
  189. }
  190. Db::connect('mysql_sys')->commit();
  191. return json_show(0, '操作成功');
  192. } catch (Exception $exception) {
  193. Db::connect('mysql_sys')->rollback();
  194. return json_show(1005, '操作失败' . $exception->getMessage());
  195. }
  196. }
  197. //修改供应商账号
  198. public static function editAccount(array $param = [])
  199. {
  200. $db = new SupplierUserModel();
  201. $res = $db
  202. ->field('uid')
  203. ->where(['uid' => $param['uid'], 'is_del' => $db::$is_del_normal])
  204. ->findOrEmpty()
  205. ->isEmpty();
  206. if ($res) return json_show(1004, '该账号不存在');
  207. $rs = $db
  208. ->where('uid', $param['uid'])
  209. ->save($param);
  210. return $rs ? json_show(0, '修改成功') : json_show(1005, '修改失败');
  211. }
  212. //修改供应商账号状态
  213. public static function statusAccount(array $param = [])
  214. {
  215. Db::connect('mysql_sys')->startTrans();
  216. try {
  217. $db = new SupplierUserModel();
  218. $res = $db
  219. ->field('uid,status')
  220. ->where([
  221. 'uid' => $param['uid'],
  222. 'is_del' => $db::$is_del_normal,
  223. ])->findOrEmpty();
  224. if ($res->isEmpty()) throw new Exception('该账号不存在');
  225. if ($res->status == $param['status']) throw new Exception('不能重复操作');
  226. $db->where([
  227. 'uid' => $param['uid'],
  228. 'is_del' => $db::$is_del_normal,
  229. ])->where('status', '<>', $param['status'])
  230. ->save(['status' => $param['status']]);
  231. SupplierRelationUserModel::where([
  232. 'uid' => $param['uid'],
  233. 'is_del' => $db::$is_del_normal,
  234. ])->where('status', '<>', $param['status'])
  235. ->save(['status' => $param['status']]);
  236. Db::connect('mysql_sys')->commit();
  237. return json_show(0, '操作成功');
  238. } catch (Exception $exception) {
  239. Db::connect('mysql_sys')->rollback();
  240. return json_show(1005, '操作失败,' . $exception->getMessage());
  241. }
  242. }
  243. //删除供应商账号
  244. public static function deleteAccount(int $uid = 0)
  245. {
  246. Db::connect('mysql_sys')->startTrans();
  247. try {
  248. $db = new SupplierUserModel();
  249. $res = $db
  250. ->field('uid')
  251. ->where([
  252. 'uid' => $uid,
  253. 'is_del' => $db::$is_del_normal,
  254. ])->findOrEmpty();
  255. if ($res->isEmpty()) throw new Exception('该账号不存在或已删除');
  256. $db->where('uid', $uid)
  257. ->where('is_del', $db::$is_del_normal)
  258. ->save(['is_del' => $db::$is_del_deleted]);
  259. SupplierRelationUserModel::where('uid', $uid)
  260. ->where('is_del', $db::$is_del_normal)
  261. ->save(['is_del' => $db::$is_del_deleted]);
  262. Db::connect('mysql_sys')->commit();
  263. return json_show(0, '删除成功');
  264. } catch (Exception $exception) {
  265. Db::connect('mysql_sys')->rollback();
  266. return json_show(1005, '删除失败,' . $exception->getMessage());
  267. }
  268. }
  269. }