UserAuditForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\SaasUser;
  4. use app\models\User;
  5. use app\models\UserAuditLog;
  6. use app\models\UserAuditSetting;
  7. use app\utils\Notice\NoticeSend;
  8. use yii\base\Model;
  9. use yii\helpers\ArrayHelper;
  10. use app\utils\Wechat\B2b\B2bPay;
  11. class UserAuditForm extends Model
  12. {
  13. public $id;
  14. public $store_id;
  15. public $status;
  16. public $bg_image;
  17. public $form_name;
  18. public $custom_form;
  19. public $protocol;
  20. public $start_time;
  21. public $end_time;
  22. public $audit_start_time;
  23. public $audit_end_time;
  24. public $nickname;
  25. public $mobile;
  26. public $ids;
  27. public $pass_remark;
  28. public $form_list;
  29. public function rules()
  30. {
  31. return [
  32. [['id', 'store_id', 'status'], 'integer'],
  33. [['bg_image', 'form_name', 'custom_form', 'protocol', 'start_time', 'end_time', 'audit_start_time',
  34. 'audit_end_time', 'nickname', 'mobile', 'ids', 'pass_remark', 'form_list'], 'string'],
  35. ];
  36. }
  37. //用户审核设置
  38. public function userAuditSetting() {
  39. try {
  40. $store_id = $this->store_id;
  41. if (\Yii::$app->request->isGet) {
  42. //
  43. $userAuditSetting = UserAuditSetting::findOne(['store_id' => $store_id]);
  44. $data = [
  45. 'id' => $userAuditSetting->id ?? 0,
  46. 'status' => (int)$userAuditSetting->status ?? 0,
  47. 'bg_image' => $userAuditSetting->bg_image ?? '',
  48. 'form_name' => $userAuditSetting->form_name ?? '',
  49. 'form_list' => json_decode($userAuditSetting->custom_form, true) ?? null,
  50. 'protocol' => $userAuditSetting->protocol ?? '',
  51. 'wx_b2b_pay_open' => $userAuditSetting->wx_b2b_pay_open ?? 0,
  52. 'wx_b2b_pay_mchid' => $userAuditSetting->wx_b2b_pay_mchid ?? '',
  53. ];
  54. return [
  55. 'code' => 0,
  56. 'msg' => '获取成功',
  57. 'mchorder' => B2bPay::mchorder($store_id),
  58. 'data' => $data + ($userAuditSetting ? $userAuditSetting->toArray() : []),
  59. ];
  60. } else {
  61. $post = all_params();
  62. $status = $this->status;
  63. $bg_image = $this->bg_image;
  64. $form_name = $this->form_name;
  65. $custom_form = $this->form_list;
  66. $protocol = $this->protocol;
  67. if (!empty($custom_form)) {
  68. $custom_form = json_decode($custom_form, true);
  69. if (!empty($custom_form)) {
  70. foreach ($custom_form as $custom) {
  71. if (!isset($custom['name'])) {
  72. throw new \Exception('自定义表单缺少名称');
  73. }
  74. if (!isset($custom['required'])) {
  75. throw new \Exception('自定义表单缺少是否必填项');
  76. }
  77. if (!isset($custom['tip'])) {
  78. throw new \Exception('自定义表单缺少提示信息');
  79. }
  80. if (!isset($custom['type'])) {
  81. throw new \Exception('自定义表单缺少类型');
  82. }
  83. }
  84. $custom_form = json_encode($custom_form, JSON_UNESCAPED_UNICODE);
  85. } else {
  86. $custom_form = null;
  87. }
  88. }
  89. $userAuditSetting = UserAuditSetting::findOne(['store_id' => $store_id]);
  90. if (!$userAuditSetting) {
  91. $userAuditSetting = new UserAuditSetting();
  92. $userAuditSetting->store_id = $store_id;
  93. }
  94. $userAuditSetting->status = $status;
  95. $userAuditSetting->bg_image = $bg_image;
  96. $userAuditSetting->form_name = $form_name;
  97. $userAuditSetting->custom_form = $custom_form;
  98. $userAuditSetting->protocol = $protocol;
  99. if($post['wx_b2b_pay_mchid'] && $userAuditSetting->wx_b2b_pay_mchid != $post['wx_b2b_pay_mchid']){
  100. $b2bPayUpdate = 1;
  101. }
  102. isset($post['wx_b2b_pay_open']) && $userAuditSetting->wx_b2b_pay_open = (int)$post['wx_b2b_pay_open'];
  103. isset($post['wx_b2b_pay_mchid']) && $userAuditSetting->wx_b2b_pay_mchid = trim($post['wx_b2b_pay_mchid']);
  104. if (!$userAuditSetting->save()) {
  105. throw new \Exception('保存失败' . json_encode($userAuditSetting->errors, JSON_UNESCAPED_UNICODE));
  106. }
  107. $b2bPayUpdate && $b2bPayUpdate = B2bPay::setmchprofitrate($store_id, B2bPay::getMinProfitRate());
  108. return [
  109. 'code' => 0,
  110. 'b2bPayUpdate' => $b2bPayUpdate,
  111. 'mchorder' => B2bPay::mchorder($store_id),
  112. 'msg' => '操作成功'
  113. ];
  114. }
  115. } catch (\Exception $e) {
  116. return [
  117. 'code' => 1,
  118. 'msg' => $e->getMessage()
  119. ];
  120. }
  121. }
  122. //用户审核列表
  123. public function userAuditList() {
  124. $nickname = $this->nickname;
  125. $mobile = $this->mobile;
  126. $start_time = $this->start_time;
  127. $end_time = $this->end_time;
  128. $audit_start_time = $this->audit_start_time;
  129. $audit_end_time = $this->audit_end_time;
  130. $status = $this->status;
  131. $id = $this->id;
  132. $query = UserAuditLog::find()->alias('ual')
  133. ->leftJoin(['u' => User::tableName()], 'u.id = ual.user_id')
  134. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  135. ->where(['ual.store_id' => $this->store_id, 'ual.is_delete' => 0]);
  136. // ->andWhere(['AND', ['IS NOT', 'u.id', null], ['IS NOT', 'su.id', null]]);
  137. if (!empty($id)) {
  138. $query->andWhere(['ual.id' => $id]);
  139. }
  140. if (!empty($nickname)) {
  141. $query->andWhere(['like', 'su.name', $nickname]);
  142. }
  143. if (!empty($mobile)) {
  144. $query->andWhere(['like', 'su.mobile', $mobile]);
  145. }
  146. if (!empty($start_time)) {
  147. $query->andWhere(['>=', 'ual.created_at', strtotime($start_time)]);
  148. }
  149. if (!empty($end_time)) {
  150. $query->andWhere(['<=', 'ual.created_at', strtotime($end_time)]);
  151. }
  152. if (!empty($audit_start_time)) {
  153. $query->andWhere(['>=', 'ual.audit_time', strtotime($audit_start_time)]);
  154. }
  155. if (!empty($audit_end_time)) {
  156. $query->andWhere(['<=', 'ual.audit_time', strtotime($audit_end_time)]);
  157. }
  158. if ($status !== null && in_array($status, [0, 1, 2])) {
  159. $query->andWhere(['ual.status' => $status]);
  160. }
  161. $query->orderBy('ual.id DESC');
  162. $query->select('ual.id, su.name nickname, su.mobile, ual.avatar, ual.status, ual.audit_time, ual.created_at, ual.pass_remark, ual.custom_form, ual.user_id');
  163. $pagination = pagination_make($query);
  164. foreach ($pagination['list'] as &$item) {
  165. $item['audit_time'] = $item['audit_time'] ? date('Y-m-d H:i:s', $item['audit_time']) : '-';
  166. $item['created_at'] = $item['created_at'] ? date('Y-m-d H:i:s', $item['created_at']) : '';
  167. $item['custom_form'] = json_decode($item['custom_form'], true);
  168. $item['status'] = (int)$item['status'];
  169. if (!$item['user_id'] || !$item['mobile']) {
  170. $item['nickname'] = '-';
  171. $item['mobile'] = '-';
  172. }
  173. }
  174. return [
  175. 'code' => 0,
  176. 'msg' => 'success',
  177. 'data' => [
  178. 'data' => $pagination['list'],
  179. 'pageNo' => $pagination['pageNo'],
  180. 'totalCount' => $pagination['totalCount'],
  181. ]
  182. ];
  183. }
  184. //用户审核拒绝/同意/删除
  185. public function userAuditStatus() {
  186. try {
  187. $ids = $this->ids;
  188. $status = intval($this->status);
  189. $pass_remark = $this->pass_remark;
  190. if (!empty($ids)) {
  191. $ids = explode(',', $ids);
  192. }
  193. if (empty($ids)) {
  194. throw new \Exception('请选择用户审核记录');
  195. }
  196. if (!in_array($status, [1, 2, 3])) {
  197. throw new \Exception('状态错误');
  198. }
  199. foreach ($ids as $id) {
  200. $userAuditLog = UserAuditLog::findOne(['id' => $id, 'is_delete' => 0]);
  201. if (!$userAuditLog) {
  202. throw new \Exception('审核记录查询失败');
  203. }
  204. //通过/拒绝
  205. if (in_array($status, [1, 2])) {
  206. if ($userAuditLog->status !== 0) {
  207. throw new \Exception('审核记录不可重复操作');
  208. }
  209. if ($status === 2 && empty($pass_remark)) {
  210. throw new \Exception('拒绝请填写审核内容');
  211. }
  212. $userAuditLog->status = $status;;
  213. $userAuditLog->pass_remark = '';
  214. $userAuditLog->audit_time = (string)time();
  215. if ($status === 2) {
  216. $userAuditLog->pass_remark = $pass_remark;
  217. }
  218. } else {
  219. $userAuditLog->is_delete = 1;
  220. }
  221. if (!$userAuditLog->save()) {
  222. throw new \Exception(json_encode($userAuditLog->errors, JSON_UNESCAPED_UNICODE));
  223. }
  224. if ($status === 1) {
  225. $user = User::findOne(['id' => $userAuditLog->user_id]);
  226. if (!empty($user)) {
  227. if (!empty($userAuditLog->avatar)) {
  228. $user->avatar_url = $userAuditLog->avatar;
  229. if (!$user->save()) {
  230. throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
  231. }
  232. }
  233. $saasUser = SaasUser::findOne(['mobile' => $user->binding]);
  234. if (!empty($saasUser)) {
  235. if (!empty($userAuditLog->avatar)) {
  236. $saasUser->avatar = $userAuditLog->avatar;
  237. if (!$saasUser->save()) {
  238. throw new \Exception(json_encode($saasUser->errors, JSON_UNESCAPED_UNICODE));
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. return [
  246. 'code' => 0,
  247. 'msg' => '操作成功'
  248. ];
  249. } catch (\Exception $e) {
  250. return [
  251. 'code' => 1,
  252. 'msg' => $e->getMessage()
  253. ];
  254. }
  255. }
  256. public static function pay($session_key, $order, $type, $goods_names = null, $balance_price = 0) {
  257. try {
  258. $store_id = $order->store_id;
  259. // $user = UserAuditLog::findOne(['user_id' => $order->user_id, 'is_delete' => 0, 'status' => UserAuditLog::STATUS_AGREE]);
  260. // if(!$user){
  261. // throw new \Exception('b2b用户状态异常');
  262. // }
  263. $userAuditSetting = UserAuditSetting::findOne(['store_id' => $store_id]);
  264. if(!$userAuditSetting['wx_b2b_pay_open']){
  265. throw new \Exception('未启用B2b门店助手支付');
  266. }
  267. // $session_key = 'QEjRQCmLrKV+bU+0X7ebuw==';
  268. $pay = B2bPay::pay($session_key, $order, $type, $goods_names, $balance_price);
  269. return $pay;
  270. } catch (\Exception $e) {
  271. return [
  272. 'code' => 1,
  273. 'msg' => $e->getMessage()
  274. ];
  275. }
  276. }
  277. }