| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Admin;
- use app\models\AggregateQrcode;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\Salesman;
- use app\models\Store;
- use app\models\StoreAliMini;
- use app\models\StoreMini;
- use app\models\WechatConfig;
- use app\utils\QrCode;
- use EasyWeChat\Factory;
- use yii\base\Model;
- use AopClient;
- use app\models\SaasCategory;
- include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
- class AggregateQrcodeForm extends Model
- {
- public $id;
- public $store_id;
- public $param_url;
- public $admin_id;
- public $is_use;
- public $wx_url;
- public $ali_url;
- public $create_time;
- public $number;
- public $name;
- public $salesman_name;
- public $status;
- public $salesman_id;
- public $ids;
- public $type;
- public function rules()
- {
- return [
- [["id", "store_id", "mini_id", "admin_id", "salesman_id", "is_use", "number",'salesman_id', 'type'],'integer'],
- [["param_url", "qrcode_url", "wx_url", "ali_url", "create_time",],'string'],
- [['ids'],'array']
- ];
- }
- public function createdUrl(){
- $num = 0;
- $id = [];
- foreach(range(1,$this->number) as $item){
- $max_id = AggregateQrcode::find()->max('id');
- $qrcode = new AggregateQrcode();
- $qrcode->create_time = time();
- $qrcode->store_id = 0;
- $qrcode->param_url = \Yii::$app->request->hostInfo.'/web/face/pay/'.($max_id+1);
- $qrcode->wx_url = "/face-pay/face-pay/payOut";
- $qrcode->ali_url = "/face-pay/face-pay/payOut";
- $res = $qrcode->save();
- if($res){
- $qrcode->param_url = \Yii::$app->request->hostInfo.'/web/face/pay/'.($qrcode->id);
- $qrcode->save();
- array_push($id, $qrcode->id);
- $num++;
- }
- }
- return [
- 'code'=>0,
- 'msg'=>'已成功生成'.$num.'条链接'
- ];
- }
- public function qrcodeList(){
- //业务员列表
- $query = Salesman::find()->alias('s')->leftJoin(['su'=>SaasUser::tableName()],'s.saas_user_id = su.id')->where(['s.is_delete'=>0]);
- $Salesman = $query->select('s.id,su.name')->asArray()->all();
- //end
- //商城列表,排除已经绑定过聚合码的商城 start
- $store_ids = AggregateQrcode::find()->where(['and',['<>','store_id',0]])->select("store_id")->column();
- if (\Yii::$app->prod_is_dandianpu()) {
- $self_mini = Option::find()->where(['name' => 'self_mini', 'group' => 'store', 'value' => 1])->select("store_id")->column();
- $stores = Store::find()->where(['not in','id',$store_ids])->andWhere(['is_delete' => 0, 'id' => $self_mini])->asArray()->select('id,name,salesman_id')->all();
- } else {
- $stores = Store::find()->where(['not in','id',$store_ids])->andWhere(['is_delete' => 0, 'business_model' => 1])->asArray()->select('id,name,salesman_id')->all();
- }
- //end
- $query = AggregateQrcode::find()->alias('aq')->leftJoin(['s'=>Store::tableName()],'aq.store_id = s.id');
- $query->leftJoin(['sm'=>Salesman::tableName()],'aq.salesman_id = sm.id');
- $query->leftJoin(['su'=>SaasUser::tableName()],'sm.saas_user_id = su.id');
- $query->leftJoin(['a'=>Admin::tableName()],'aq.admin_id = a.id');
- $query->leftJoin(['c' => SaasCategory::tableName()], 's.category_id = c.id');
- if(!empty($this->name)){
- $query->andWhere(['LIKE','s.name',$this->name]);
- }
- if(!empty($this->salesman_name)){
- $query->andWhere(['LIKE','sm.name',$this->salesman_name]);
- }
- switch ($this->status){
- case 1:
- $query->andWhere(['shop_bind_time'=>'']);
- break;
- case 2:
- $query->andWhere(['<>','shop_bind_time','']);
- break;
- case 3:
- $query->andWhere(['and',['<>','shop_bind_time',''],['<>','mini_time','']]);
- break;
- }
- $query = $query->select('s.name store_name,aq.id,aq.store_id,aq.admin_id,aq.salesman_id,aq.is_use,aq.shop_bind_time,aq.mini_time,su.name salesman_name,a.name admin_name,s.logo as store_logo,c.name as category_name, aq.wx_status, aq.ali_status')->orderBy('id desc');
- $pagination = pagination_make($query);
- foreach($pagination['list'] as &$item){
- if(empty($item['shop_bind_time'])){
- $item['status']=1;
- }
- if(!empty($item['shop_bind_time'])){
- $item['status']=2;
- }
- if(!empty($item['shop_bind_time']) && !empty($item['mini_time'])){
- $item['status']=3;
- }
- $item['business_no'] = null;
- $item['wx_status'] *= 1;
- $item['ali_status'] *= 1;
- $wechatConfig = WechatConfig::find()->where(['store_id' => $item['store_id']])->select('mch_id')->one();
- if ($wechatConfig) {
- $item['business_no'] = $wechatConfig->mch_id;
- }
- }
- return [
- 'code'=>0,
- 'msg' =>"获取成功",
- 'data' => $pagination['list'],
- 'store_list'=>$stores,//商城列表
- 'salesman_list'=>$Salesman,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ];
- }
- public function urlDel(){
- $t = \Yii::$app->db->beginTransaction();
- try {
- if(empty($this->ids)){
- throw new \Exception("没有选择数据");
- }
- $res = AggregateQrcode::deleteAll(['id'=>$this->ids]);
- if($res){
- $t->commit();
- return [
- 'code'=>0,
- 'msg'=>"删除成功"
- ];
- }else{
- throw new \Exception("删除失败");
- }
- }catch (\Exception $e){
- $t->rollBack();
- return [
- 'code'=>0,
- 'msg'=>$e->getMessage()
- ];
- }
- }
- //绑定商城
- public function bindStore(){
- try {
- $ag = AggregateQrcode::findone($this->id);
- if(!empty($ag)){
- //绑定商城
- if(!empty($this->store_id)){
- $st_ag = AggregateQrcode::findone(['store_id' => $this->store_id]);
- if(!empty($st_ag)){
- throw new \Exception("此商城已经被绑定");
- }
- if(!empty($ag->store_id)){
- throw new \Exception("已经绑定过商城");
- }
- $store = Store::find()->where(['id'=>$this->store_id])->select("id,salesman_id")->one();
- if(!empty($store->salesman_id)){
- $Salesman = Salesman::findOne($store->salesman_id);
- $ag->salesman_id = $store->salesman_id;
- $ag->admin_id = $Salesman->admin_id;
- }
- $ag->store_id = $this->store_id;
- $ag->shop_bind_time = time();
- if(!$ag->save()){
- throw new \Exception("商城绑定失败");
- }
- return [
- 'code' => 0,
- 'msg' => "绑定成功"
- ];
- }
- if(empty($ag->store_id)){
- throw new \Exception("请先绑定商城");
- }
- throw new \Exception("没有传入任何可用参数");
- }else{
- throw new \Exception("没有找到二维码信息");
- }
- }catch (\Exception $e){
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage()
- ];
- }
- }
- public function qrcodeBindInfo(){
- try {
- $aq = AggregateQrcode::findOne(['store_id'=>get_store_id()]);
- $w_mini = StoreMini::find()->where(['store_id'=>get_store_id()])->select("id,appid")->orderBy('id desc')->one();
- $ali_mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->select("id,auth_app_id")->orderBy('id desc')->one();
- if(empty($aq)){
- return [
- 'code'=>1,
- 'msg'=>"未查找到任何聚合码信息"
- ];
- }
- $data['wx_status'] = empty($w_mini->appid)?'-1':$aq->wx_status;
- $data['ali_status'] = empty($ali_mini->auth_app_id)?'-1':$aq->ali_status;
- return [
- 'code'=>0,
- 'msg'=>"查询成功",
- 'data'=>$data
- ];
- }catch (\Exception $e){
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage()
- ];
- }
- }
- //绑定普通二维码
- public function bindQrcode()
- {
- try {
- $type = (int)$this->type;
- $id = $this->id;
- $qrcode = AggregateQrcode::findOne($id);
- if (empty($qrcode)) {
- throw new \Exception("未找到聚合码信息");
- }
- if (empty($qrcode->store_id)) {
- throw new \Exception("请绑定商城信息");
- }
- //如果是微信方式
- if ($type === 1) {
- //判断是否已经绑定微信 但二维码未绑定
- if ((int)$qrcode->wx_status === 0) {
- if (!empty($qrcode->wx_mini_id)) {
- $store_mini = StoreMini::findOne($qrcode->wx_mini_id);
- } else {
- $store_mini = StoreMini::findOne(['store_id' => $qrcode->store_id, 'is_cancle' => 0, 'is_use' => 1]);
- if (empty($store_mini)) {
- throw new \Exception("需要使用三方注册小程序或查看小程序是否已经上线使用");
- }
- $qrcode->wx_mini_id = $store_mini->id;
- if (!$qrcode->save()) {
- throw new \Exception(json_encode($qrcode->errors));
- }
- }
- if (empty($store_mini)) {
- throw new \Exception("需要使用三方注册小程序");
- }
- $form = new WechatThirdForm();
- $form->bind_store_id = $qrcode->store_id;
- $result = $form->downQrcodeRules($store_mini->id, $store_mini->appid);
- if ($result['code'] !== 0) {
- throw new \Exception($result['msg']);
- }
- return $result;
- } else {
- throw new \Exception("已经绑定聚合码信息");
- }
- } else {
- if ((int)$qrcode->ali_status === 0) {
- if (!empty($qrcode->ali_mini_id)) {
- $store_mini = StoreAliMini::findOne(['id' => $qrcode->ali_mini_id, 'is_cancel' => 0]);
- } else {
- $store_mini = StoreAliMini::findOne(['store_id' => $qrcode->store_id, 'is_cancel' => 0, 'is_use' => 1]);
- if (empty($store_mini)) {
- throw new \Exception("需要使用三方注册小程序或查看小程序是否已经上线使用");
- }
- $qrcode->ali_mini_id = $store_mini->id;
- if (!$qrcode->save()) {
- throw new \Exception(json_encode($qrcode->errors));
- }
- }
- if (empty($store_mini)) {
- throw new \Exception("需要使用三方注册小程序");
- }
- $form = new AlipayThirdForm();
- $result = $form->bindQrcode($store_mini->id);
- if ($result['code'] !== 0) {
- throw new \Exception($result['msg']);
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功',
- 'data' => $result
- ];
- } else {
- throw new \Exception("已经绑定聚合码信息");
- }
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|