| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\controllers\v1;
- use app\models\Clerk;
- use app\models\Coupon;
- use app\models\Goods;
- use app\models\MdStaff;
- use app\models\SaasProfitCash;
- use app\models\SaasUser;
- use app\models\SharingReceiver;
- use app\models\User;
- use app\models\UserCoupon;
- use app\modules\client\controllers\BaseController;
- use app\modules\client\models\v1\PlatformLoginForm;
- use app\plugins\integral\models\SaasIntegralOrder;
- use app\plugins\scanCodePay\models\Order;
- use app\plugins\scanCodePay\models\OrderDetail;
- use app\utils\Ocr\OcrApi;
- use app\utils\OrderNo;
- use yii\base\BaseObject;
- use yii\data\Pagination;
- use app\models\Store;
- use app\models\Option;
- use app\models\SaasHistory;
- use yii\helpers\Json;
- class SaasController extends BaseController
- {
- /**
- * 福利中心订单,目前只有当面付订单
- */
- public function actionOrderList() {
- $page = get_params('page', 1);
- $limit = get_params('limit', 10);
- $query = Order::find()->where(['store_id' => get_store_id(), 'user_id' => get_user_id(), 'is_delete' => 0]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $list = $query->select('id, pay_price, order_no, is_pay, created_at')->limit($pagination->limit)
- ->offset($pagination->offset)
- ->asArray()->orderBy(['created_at' => SORT_DESC])->all();
- // $count = count($scan_order); // 总条数
- // $start = ($page - 1) * $limit; // 偏移量,当前页-1乘以每页显示条数
- // $order = array_slice($scan_order, $start, $limit);
- foreach ($list as $key => $value) {
- $list[$key]['goods_name'] = '当面付';
- $goods_id = OrderDetail::findOne(['order_id' => $value['id']])->goods_id;
- $list[$key]['goods_pic'] = Goods::findOne($goods_id)->cover_pic;
- $list[$key]['type'] = 'scan';
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ]
- ]);
- }
- /**
- * 核销员待核销订单,目前只有积分兑换订单 2021-06-30
- */
- public function actionNotClerkOrderList() {
- // $current_user = User::findOne(['id' => get_user_id(), 'is_delete' => User::USER_NOT_DELETE, 'is_saas_clerk' => 1, 'store_id' => get_store_id()]);
- $current_user = MdStaff::findOne(['store_id' => get_store_id(), 'saas_user_id' => get_saas_user_id(), 'is_delete' => 0]);
- if (!$current_user) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '您不是核销员,无法核销订单!'
- ]);
- }
- $page = get_params('page', 1);
- $limit = get_params('limit', 10);
- $user_id = get_params('user_id');
- // $user = User::findOne($user_id);
- // $clerk = Clerk::findOne(['user_id' => get_user_id(), 'is_delete' => 0]);
- // if (!$user || !$clerk) {
- // return $this->asJson([
- // 'code' => 0,
- // 'msg' => 'success',
- // 'data' => [
- // 'row_count' => 0,
- // 'page_count' => 0,
- // 'list' => [],
- // ]
- // ]);
- // }
- $query = SaasIntegralOrder::find()->where(['store_id' => get_store_id(), 'user_id' => $user_id, 'is_clerk' => 0, 'is_delete' => 0]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $list = $query->select('*')->limit($pagination->limit)
- ->offset($pagination->offset)
- ->asArray()->orderBy(['created_at' => SORT_DESC])->all();
- foreach ($list as $key => $value) {
- $goods = Goods::findOne($value['goods_id']);
- $list[$key]['goods_pic'] = $goods->cover_pic;
- $list[$key]['goods_name'] = $goods->name;
- $list[$key]['type'] = 'integral';
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ]
- ]);
- }
- /**
- * 联盟订单,目前只有积分兑换订单 2021-06-30
- */
- public function actionLeagueOrderList() {
- $page = get_params('page', 1);
- $limit = get_params('limit', 10);
- $user_id = get_saas_user_id();
- $query = SaasIntegralOrder::find()->where(['user_id' => $user_id, 'is_delete' => 0]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $list = $query->select('*')->limit($pagination->limit)
- ->offset($pagination->offset)
- ->asArray()->orderBy(['created_at' => SORT_DESC])->all();
- foreach ($list as $key => $value) {
- $goods = Goods::findOne($value['goods_id']);
- $list[$key]['goods_pic'] = $goods->cover_pic;
- $list[$key]['goods_name'] = $goods->name;
- $list[$key]['type'] = 'integral';
- $list[$key]['shop'] = [
- 'latitude' => '',
- 'longitude' => '',
- 'address' => '',
- ];
- // 获取商城坐标信息
- $store = Store::findOne($value['store_id']);
- if ($store) {
- if (!empty($store->coordinate)) {
- list($latitude, $longitude) = explode(',', $store->coordinate);
- $list[$key]['shop']['latitude'] = $latitude;
- $list[$key]['shop']['longitude'] = $longitude;
- }
- $list[$key]['shop']['address'] = $store->address;
- $list[$key]['shop']['name'] = $store->name;
- $list[$key]['shop']['logo'] = $store->logo;
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ]
- ]);
- }
- /**
- * 核销订单
- */
- public function actionClerkOrder() {
- $type = get_params('type');
- $id = get_params('id');
- $store_id = get_store_id();
- $user_id = get_user_id();
- // $current_user = User::findOne(['id' => $user_id, 'is_delete' => User::USER_NOT_DELETE, 'is_saas_clerk' => 1, 'store_id' => $store_id]);
- $current_user = MdStaff::findOne(['store_id' => $store_id, 'saas_user_id' => get_saas_user_id(), 'is_delete' => 0]);
- if (!$current_user) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '您不是核销员,无法核销订单!'
- ]);
- }
- switch ($type) {
- case 'integral':
- $integral_order = SaasIntegralOrder::findOne(['id' => $id, 'is_delete' => 0]);
- if (!$integral_order) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '订单不存在'
- ]);
- }
- if ($integral_order->is_clerk == 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '订单已核销,无法再次核销'
- ]);
- }
- SaasIntegralOrder::updateAll(['is_clerk' => 1, 'clerk_id' => $user_id], ['store_id' => $store_id, 'id' => $id]);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '核销成功'
- ]);
- default:
- return $this->asJson([
- 'code' => 1,
- 'msg' => 'fail'
- ]);
- }
- }
- /**
- * 删除订单
- */
- public function actionDeleteOrder() {
- $type = get_params('type');
- $id = get_params('id');
- $store_id = get_store_id();
- switch ($type) {
- case 'scan':
- Order::updateAll(['is_delete' => 1], ['store_id' => $store_id, 'id' => $id]);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success'
- ]);
- case 'integral':
- SaasIntegralOrder::updateAll(['is_delete' => 1], ['store_id' => $store_id, 'id' => $id]);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success'
- ]);
- default:
- return $this->asJson([
- 'code' => 1,
- 'msg' => 'fail'
- ]);
- }
- }
- /**
- * 订单详情
- */
- public function actionOrderDetail() {
- $type = get_params('type');
- $id = get_params('id');
- $order = [];
- switch ($type) {
- case 'scan':
- $scan_temp = Order::findOne(['id' => $id, 'is_delete' => 0]);
- $order['goods_name'] = '当面付';
- $goods_id = OrderDetail::findOne(['order_id' => $scan_temp->id])->goods_id;
- $order['goods_pic'] = Goods::findOne($goods_id)->cover_pic;
- $order['cost'] = $scan_temp->pay_price;
- $order['order_no'] = $scan_temp->order_no;
- $order['coupon'] = Coupon::findOne(UserCoupon::findOne($scan_temp->user_coupon_id)->coupon_id);
- $order['status'] = $scan_temp->is_pay == 1 ? '已支付' : '未支付';
- $order['created_at'] = $scan_temp->created_at;
- break;
- case 'integral':
- $integral_temp = SaasIntegralOrder::findOne(['id' => $id, 'is_delete' => 0]);
- $goods = Goods::findOne($integral_temp->goods_id);
- $order['goods_name'] = $goods->name;
- $order['goods_pic'] = $goods->cover_pic;
- $order['cost'] = $integral_temp->cost;
- $order['order_no'] = $integral_temp->order_no;
- $order['status'] = $integral_temp->is_clerk == 1 ? '已核销' : '待核销';
- $order['created_at'] = $integral_temp->created_at;
- break;
- default:
- return $this->asJson([
- 'code' => 1,
- 'msg' => 'fail'
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $order
- ]);
- }
- /**
- * 个人分账记录
- * @return \yii\web\Response
- */
- public function actionShareProfitRecord() {
- $page = get_params('page', 1);
- $limit = get_params('limit', 10);
- $user = get_user();
- $query = SharingReceiver::find();
- if (\Yii::$app->isSaas()) {
- $users = User::find()->where(['binding' => $user->binding])->select('wechat_open_id')->asArray()->all();
- $users_open_id = array_column($users, 'wechat_open_id');
- $query->where(['in', 'account', $users_open_id]);
- } else {
- $query->where(['store_id' => $user->store_id, 'account' => $user->wechat_open_id]);
- }
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $list = $query->select('*')->limit($pagination->limit)
- ->offset($pagination->offset)
- ->asArray()->orderBy(['created_at' => SORT_DESC])->all();
- foreach ($list as $key => $value) {
- $tmp = [];
- $tmp['order_no'] = $value['order_no'];
- $tmp['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- $tmp['amount'] = $value['amount'];
- $tmp['remark'] = $value['remark'];
- $tmp['pay_status'] = $value['is_pay'] == 0 ? '待分账' : ($value['is_pay'] == 1 ? '已分账' : '分账失败');
- $list[$key] = $tmp;
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ]
- ]);
- }
- /**
- * 联盟佣金提现提交
- */
- public function actionCashCommit() {
- $amount = post_params('amount');
- if ($amount < 0.01) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现最小金额不能小于0.01元'
- ]);
- }
- $user = !empty(get_user()) ? get_user() : User::find()->where(['binding'=>get_saas_user()->mobile])->one();
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- if ($saas_user->share_profit < $amount) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '当前提现金额大于可提现联盟佣金'
- ]);
- }
- if (!$saas_user->platform_open_id) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请先绑定平台小程序openid'
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- $cash = new SaasProfitCash();
- $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_SAAS_PROFIT_CASH);
- $cash->user_id = get_saas_user_id();
- $cash->mobile = $user->binding;
- $cash->amount = $amount;
- $cash->status = SaasProfitCash::CASH_STATUS_WAIT;
- $cash->created_at = time();
- if (!$cash->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $cash->errors[0]
- ]);
- }
- $cash->before_price = $saas_user->share_profit;
- $cash->after_price = ($saas_user->share_profit - $amount);
- $saas_user->share_profit = $saas_user->share_profit - $amount;
- $saas_user->save();
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '提交成功,请等待审核'
- ]);
- }
- /**
- * 联盟佣金提现列表
- */
- public function actionCashList() {
- $page = get_params('page', 1);
- $limit = get_params('limit', 10);
- $user = get_user();
- $query = SaasProfitCash::find()->where(['mobile' => !empty($user->binding) ? $user->binding : get_saas_user()->mobile]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $list = $query->select('*')->limit($pagination->limit)
- ->offset($pagination->offset)
- ->asArray()->orderBy(['created_at' => SORT_DESC])->all();
- foreach ($list as &$value) {
- $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- $value['status_text'] = $value['status'] == 0 ? '待审核' : ($value['status'] == 1 ? '已通过' : '已拒绝');
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ]
- ]);
- }
- public function actionCheck() {
- $phone = get_params('phone');
- if (empty($phone)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数有误'
- ]);
- }
- $share_user = SaasUser::findOne(['mobile' => $phone]);
- if (!$share_user || empty($share_user->platform_open_id)) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => 0
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => 1
- ]);
- }
- public function actionPlatformLogin() {
- $form = new PlatformLoginForm();
- $form->store_id = get_store_id();
- $form->attributes = get_params();
- return $this->asJson($form->login());
- }
- public function actionSearchKeyword()
- {
- $keyword = Option::get('search_keyword', 0, 'saas', json_encode([]));
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => json_decode($keyword['value']),
- ]);
- }
- public function actionAddHistory()
- {
- $saas_user_id = get_saas_user_id();
- if ($saas_user_id > 0) {
- $store_id = get_store_id();
- $history = SaasHistory::find()->where(['user_id' => $saas_user_id, 'store_id' => $store_id])->one();
- if ($history) {
- $history->updated_at = time();
- $history->save();
- } else {
- $history = new SaasHistory();
- $history->user_id = $saas_user_id;
- $history->store_id = $store_id;
- $history->updated_at = time();
- $history->save();
- }
- }
-
- return $this->asJson([
- 'code' => 0,
- 'msg' => '添加成功',
- ]);
- }
- public function actionGetHistory()
- {
- $page = get_params('page', 1);
- $limit = 10;
- $query = SaasHistory::find()->alias('sh')
- ->leftJoin(['s' => Store::tableName()], 'sh.store_id=s.id')
- ->select(['s.id', 's.name', 's.logo']);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $query->limit($limit)->offset($pagination->offset);
- $list = $query->orderBy(['sh.updated_at' => SORT_DESC])->asArray()->all();
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- ]);
- }
- public function actionOtherInfo() {
- $store_apply_banner = Option::get('store_apply_banner', 0, 'saas', '')['value'];
- $admin_copyright = Option::get('store_apply_agreement', 0, 'saas', '')['value'];
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'store_apply_banner' => $store_apply_banner ? Json::decode($store_apply_banner) : [],
- 'store_apply_agreement' => $admin_copyright
- ]
- ]);
- }
- /**
- * 获取信息 与ocr/info接口内容一致
- * @return \yii\web\Response
- */
- public function actionOcrInfo() {
- $type = post_params('type');
- if (empty($type) || !in_array($type, OcrApi::$validType)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '类型参数错误'
- ]);
- }
- $side = post_params('side');
- if (empty($side) && $type == OcrApi::TYPE_ID_CARD) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '身份证参数有误'
- ]);
- }
- $url = post_params('url');
- if (empty($url)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '图片地址为空'
- ]);
- }
- try {
- $ocr = new OcrApi(true);
- switch ($type) {
- case OcrApi::TYPE_LICENSE:
- $res = $ocr->getBusinessLicense($url);
- break;
- case OcrApi::TYPE_ID_CARD:
- $res = $ocr->getIDCard($url, $side);
- break;
- case OcrApi::TYPE_BANK:
- $res = $ocr->getBankCard($url);
- break;
- case OcrApi::TYPE_TEXT:
- $res = $ocr->getTextRecognition($url, 1);
- break;
- }
- if ($res['code'] == 0 && !empty($res['data'])) {
- return $this->asJson($res);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '获取信息失败'
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- }
|